#-*- coding: utf-8 -*-
import string, random
from django.contrib.auth.models import User

# Patching User for support xunit testing
def user_any():
    result = User()
    result.first_name = any_string(10)
    result.last_name = any_string(10)
    result.email = any_email()
    return result

User.any = staticmethod(user_any)

def any_int(min_value=0, max_value=1000):
    return random.randrange(min_value, max_value, 1)


def any_string(max_length=100):
    return "".join([random.choice(string.letters) 
                    for x in xrange(0, any_int(1,max_length))])


def any_email():
    return "%s@%s.%s" % (any_string(max_length=20), 
                         any_string(max_length=10), 
                         any_string(max_length=3))


def select_weighted(choices):
    """
    Supposes that choices is sequence of two elements items,
    where first one is the probability and second is the
    result object or callable
    """
    total = sum([item[0] for item in choices])
    choice = any_int(0, total)
    
    for weight, item in choices:
        if choice <= weight:
            if callable(item):
                return item()
            return item
        choice -= weight
    
    assert False

